package com.wn.sddfp.system.controller;

import cn.hutool.core.util.RandomUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import com.wn.bank.access.api.TradeLogApi;
import com.wn.sddfp.common.model.dto.bank.CardBindDto;
import com.wn.sddfp.common.model.dto.bank.SignatureDto;
import com.wn.sddfp.common.model.dto.user.LoginDto;
import com.wn.sddfp.common.model.dto.user.UserInfoVo;
import com.wn.sddfp.common.model.dto.user.UserRegisterDto;
import com.wn.sddfp.common.model.exception.BusinessException;
import com.wn.sddfp.common.model.result.Result;
import com.wn.sddfp.common.model.result.enums.impl.ResultCode;
import com.wn.sddfp.common.model.vo.LoginVo;
import com.wn.sddfp.system.service.IUserService;
import com.wn.sddfp.system.utils.TokenUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/demo01")
    public String test(HttpServletRequest request){

        String userId = request.getHeader("userId");
        System.out.println("userId = " + userId);

        String headerUserId = TokenUtils.getHeaderUserId();
        System.out.println("headerUserId = " + headerUserId);

        return "system test";
    }

    /**
     *  @Valid  启用参数校验
     * @param userRegisterDto
     * @return
     *   1、UserRegisterDto 的工具的封装
     *   2、Result的结果的统一返回
     *   3、@Valid + 全局参数异常处理
     */
    @PostMapping("/regist")
    public Result register(@RequestBody @Valid UserRegisterDto userRegisterDto){
        userService.register(userRegisterDto);
        return Result.ok();
    }


    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = userService.login(loginDto);
        return Result.ok(loginVo);
    }

    /**
     * 用户绑卡
     */
    @PostMapping("/bank/card/add")
    public Result<SignatureDto> addBankAccount(@RequestBody CardBindDto cardBindDto) {
        SignatureDto signatureDto = userService.addBankAccount(cardBindDto);
        return Result.ok(signatureDto);
    }

    @Autowired
    private TradeLogApi tradeLogApi;

    @GetMapping("/feign/{id}")
    public Result testOpenfeign(@PathVariable("id") Integer id){
        Result result = tradeLogApi.test01(id);
        return result;
    }

//    @GetMapping("/feign/{id}")
//    @HystrixCommand(fallbackMethod = "errorFallback",commandProperties = {
//            // 是否开启断路器
//            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED,value = "true"),
//            // 请求错误率大于 50% 就启动熔断器
//            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE,value = "50"),
//            // 默认10秒内访问接口达到20次，且错误率大于50%则打开断路器，触发熔断； 为了方便测试，这里修改请求数为10
//            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD,value = "10"),
//            //断路多久以后开始尝试是否恢复，默认5s
//            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS,value = "10000")
//    }) //开启 服务降级
//    public Result testOpenfeign(@PathVariable("id") Integer id){
//        if (id < 10) {
//            throw new BusinessException();
//        }
//        Result result = tradeLogApi.test01(id);
//        return result;
//    }

//    // 降级方法
//    public Result errorFallback(@PathVariable("id")Integer id){
//        return Result.fail("降级方法");
//    }

    @GetMapping("/find/{id}")
    public String find(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        String code = RandomUtil.randomNumbers(4);
        return  code + ":" + id;
    }


    @ApiOperation("根据用户id查询")
    @GetMapping("/inner/findById/{userId}")
    public Result<UserInfoVo> findUserById(@PathVariable("userId")Long userId){
        //通过id 查询user对象
        UserInfoVo userInfoVo = userService.findUserById(userId);
        return Result.ok(userInfoVo);
    }
}
